﻿using System;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Threading;
using System.Diagnostics;

namespace OAHelper
{
    public partial class SignIn : Form
    {
        public OALogin oa = new OALogin();
        public static string CurrentTime = null;
        public string SignStateHTML = null;
        public string USERNAME = ConfigurationManager.AppSettings["USERNAME"].ToString();
        public string PASSWORD = ConfigurationManager.AppSettings["USERPASSWORD"].ToString();
        public string QDTime1 = ConfigurationManager.AppSettings["OA_SIGNIN_MORNING_OK_URL"].ToString();
        public string QDTime2 = ConfigurationManager.AppSettings["OA_SIGNIN_NOON_OK_URL"].ToString();
        public string QDTime3 = ConfigurationManager.AppSettings["OA_SIGNIN_AFTERNOON_OK_URL"].ToString();
        public string QDTime4 = ConfigurationManager.AppSettings["OA_SIGNIN_NIGHT_OK_URL"].ToString();

        public delegate void GetHTMLDelegate(string html);
        public SignIn()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 最小化到正常
        /// </summary>
        private void MinimizedToNormal()
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            //this.notifyIcon1.Visible = false;
            this.ShowInTaskbar = true;
        }
        /// <summary>
        /// 正常到最小化
        /// </summary>
        private void NormalToMinimized()
        {
            this.WindowState = FormWindowState.Minimized;
            this.Visible = false;
            //this.notifyIcon1.Visible = true;
            this.notifyIcon1.ShowBalloonTip(19, "提示", "程序最小化在后台运行打卡中...", ToolTipIcon.Info);
            this.ShowInTaskbar = false;
        }
        private void SignIn_Load(object sender, EventArgs e)
        {
            Random random1 = new Random();
            txtSession.Text = OALogin.OACookies;
            GetPersonCount();
            UpdateOpeater();
            timerheartbeat.Enabled = true;
            timerCheckEnable.Enabled = true;
            QDTime1 = QDTime1 + random1.Next(2, 5) + ":" + random1.Next(0, 5) + random1.Next(0, 9);
            QDTime2 = QDTime2 + random1.Next(2, 5) + ":" + random1.Next(0, 5) + random1.Next(0, 9);
            QDTime3 = QDTime3 + random1.Next(2, 5) + ":" + random1.Next(0, 5) + random1.Next(0, 9);
            QDTime4 = QDTime4 + random1.Next(2, 5) + ":" + random1.Next(0, 5) + random1.Next(0, 9);

        }

        private void SignIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            NormalToMinimized();
        }

        private void btnSign1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认打卡？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_MORNING_URL, OALogin.OACookies);
                //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_MORNING_URL, "", Encoding.Default, OALogin.OACookies);
                txtLog.Text = msg;
                SignStateHTML = msg;
            }
        }
        private void btnSign2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认打卡？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_NOON_URL, OALogin.OACookies);
                //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_NOON_URL, "", Encoding.Default, OALogin.OACookies);
                txtLog.Text = msg;
                SignStateHTML = msg;
            }
        }
        private void btnSign3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认打卡？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_AFTERNOON_URL, OALogin.OACookies);
                //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_AFTERNOON_URL, "", Encoding.Default, OALogin.OACookies);
                txtLog.Text = msg;
                SignStateHTML = msg;
            }
        }
        private void btnSign4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认打卡？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_NIGHT_URL, OALogin.OACookies);
                //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_NIGHT_URL, "", Encoding.Default, OALogin.OACookies);
                txtLog.Text = msg;
                SignStateHTML = msg;
            }
        }

        private void timerheartbeat_Tick(object sender, EventArgs e)
        {
            GetPersonCount();
        }
        /// <summary>
        /// 获取当前在线人数
        /// </summary>
        public void GetPersonCount()
        {
            string PersonNum = oa.httpasync.OARequestGet(OALogin.OA_COUNT_URL, OALogin.OACookies);
            //string PersonNum = oa.httpcore.OASignIn(OALogin.OA_COUNT_URL, "", Encoding.Default, OALogin.OACookies);
            txtLog.Text = oa.httpcore.Log("在线人数：" + PersonNum + "人");
        }
        /// <summary>
        /// 到点自动打卡开始。
        /// </summary>
        public void StartSignInOK()
        {
            string QDCurrentTime = DateTime.Now.ToString("HH:mm:ss");
            string time1 = QDTime1;
            string time2 = QDTime2;
            string time3 = QDTime3;
            string time4 = QDTime4;
            lbltime1.Text = time1;
            lbltime2.Text = time2;
            lbltime3.Text = time3;
            lbltime4.Text = time4;

            if (QDCurrentTime == time1)
            {
                if (ckbSign830.Checked)
                {
                    string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_MORNING_URL, OALogin.OACookies);
                    //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_MORNING_URL, "", Encoding.Default, OALogin.OACookies);
                    txtLog.Text = msg;
                    SignStateHTML = msg;
                    notifyIcon1.ShowBalloonTip(25, "成功", "【早上上班】" + time1 + " 打卡已完成！", ToolTipIcon.Info);
                    label1.Text = "【早上上班】" + time1 + " 打卡已完成！";
                }

            }
            if (QDCurrentTime == time2)
            {
                if (ckbSign1135.Checked)
                {
                    string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_NOON_URL, OALogin.OACookies);
                    //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_NOON_URL, "", Encoding.Default, OALogin.OACookies);
                    txtLog.Text = msg;
                    SignStateHTML = msg;
                    notifyIcon1.ShowBalloonTip(25, "成功", "【中午下班】" + time2 + " 打卡已完成！", ToolTipIcon.Info);
                    label1.Text = "【中午下班】" + time2 + " 打卡已完成！";
                }

            }
            if (QDCurrentTime == time3)
            {
                if (ckbSign1215.Checked)
                {
                    string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_AFTERNOON_URL, OALogin.OACookies);
                    //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_AFTERNOON_URL, "", Encoding.Default, OALogin.OACookies);
                    txtLog.Text = msg;
                    SignStateHTML = msg;
                    notifyIcon1.ShowBalloonTip(25, "成功", "【下午上班】" + time3 + " 打卡已完成！", ToolTipIcon.Info);
                    label1.Text = "【下午上班】" + time3 + " 打卡已完成！";
                }

            }
            if (QDCurrentTime == time4)
            {
                if (ckbSign1715.Checked)
                {
                    string msg = oa.httpasync.OARequestGet(OALogin.OA_SIGNIN_NIGHT_URL, OALogin.OACookies);
                    //string msg = oa.httpcore.OASignIn(OALogin.OA_SIGNIN_NIGHT_URL, "", Encoding.Default, OALogin.OACookies);
                    txtLog.Text = msg;
                    SignStateHTML = msg;
                    notifyIcon1.ShowBalloonTip(25, "成功", "【晚上下班】" + time4 + " 打卡已完成！", ToolTipIcon.Info);
                    label1.Text = "【晚上下班】" + time4 + " 打卡已完成！";
                }

            }


        }
        public void UpdateOpeater()
        {
            CurrentTime = DateTime.Now.ToString("HH:mm:ss");
            DateTime currtime = Convert.ToDateTime(CurrentTime);
            DateTime time1 = Convert.ToDateTime("08:00:33");
            DateTime time2 = Convert.ToDateTime("11:30:00");
            DateTime time3 = Convert.ToDateTime("12:01:56");
            DateTime time4 = Convert.ToDateTime("17:01:34");
            DateTime time0 = Convert.ToDateTime("23:35:30");
            DateTime time01 = Convert.ToDateTime("23:50:00");

            bool a = ((time1 < currtime) && (currtime < time2));
            if (a)
            {
                btnSign1.Enabled = true;
                btnSign2.Enabled = false;
                btnSign3.Enabled = false;
                btnSign4.Enabled = false;

            }
            bool b = ((time2 < currtime) && (currtime < time3));
            if (b)
            {
                btnSign1.Enabled = false;
                btnSign2.Enabled = true;
                btnSign3.Enabled = false;
                btnSign4.Enabled = false;

            }
            bool c = ((time3 < currtime) && (currtime < time4));
            if (c)
            {
                btnSign1.Enabled = false;
                btnSign2.Enabled = false;
                btnSign3.Enabled = true;
                btnSign4.Enabled = false;

            }
            bool d = ((time4 < currtime));
            if (d)
            {
                btnSign1.Enabled = false;
                btnSign2.Enabled = false;
                btnSign3.Enabled = false;
                btnSign4.Enabled = true;

            }
            //bool e = (CurrentTime == "23:45:14" || CurrentTime == "23:48:14");
            //lblhandtips.Text = e.ToString();

            this.lblhandtips.Text = "当前时间：" + oa.httpcore.Log("");
        }

        private void timerCheckEnable_Tick(object sender, EventArgs e)
        {
            UpdateOpeater();
            StartSignInOK();
        }

        private void SignIn_Resize(object sender, EventArgs e)
        {
            if (this.WindowState != FormWindowState.Minimized)
            {
                MinimizedToNormal();
            }
            else
            {
                NormalToMinimized();
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.WindowState == FormWindowState.Minimized)
                {
                    MinimizedToNormal();
                }
                else
                {
                    NormalToMinimized();
                }
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            string PersonNum = oa.httpasync.OARequestGet(OALogin.OA_COUNT_URL, OALogin.OACookies);
            //string PersonNum = oa.httpcore.GetPersonNumber(OALogin.OA_COUNT_URL, OALogin.OACookies);
            txtLog.Text = oa.httpcore.Log("在线人数：" + PersonNum + "人");
            //string html = oa.httpcore.TestAsync("http://www.qq.com");
            //txtLog.Text = oa.httpcore.Log("【HTML】" + html); 
            //Thread testThread = new Thread(new ThreadStart(TestThreadMethod));
            //testThread.Start();
        }
        private void TestThreadMethod()
        {
            string html = oa.httpcore.OALoginPost("http://www.qq.com", "", Encoding.Default);
            if (txtLog.InvokeRequired)
            {
                txtLog.Invoke(new GetHTMLDelegate(SetHtmlToText), html);
            }
        }
        public void SetHtmlToText(string html)
        {
            txtLog.Text = html;
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
            
        }

        private void tabControl1_Click(object sender, EventArgs e)
        {
            
        }

        private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            string html;
            switch (e.TabPage.Text)
            {
                case "我的任务":
                    html = oa.httpasync.OARequestPost(OALogin.OA_TASK_URL, "FINISH_FLAG=N&RANGE=0&PROJ_ID=0",OALogin.OACookies, Encoding.Default);
                    wbie.DocumentText = html;
                    break;
                case "我的签到":
                    html = oa.httpasync.OARequestGet(OALogin.OA_MYSIGNIN_URL,OALogin.OACookies);
                    rtxtSignin.Clear();
                    rtxtSignin.AppendText(html);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 登录到IE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGotoIE_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            process.StartInfo.FileName = "iexplore.exe";
            process.StartInfo.Arguments = "http://oa.hdsxtech.com:8080/logincheck.php?UNAME=" + OALogin.name + "&PASSWORD=" + OALogin.pass;
            process.Start();
        }
    }
}
